package kr.co.boogui.dao;

import java.util.List;

import mybatis3.helper.MVCSqlSessionFactory;

import org.apache.ibatis.session.SqlSession;

import kr.co.boogui.model.NewsVO;
import kr.co.boogui.model.ParamVO;

public class NewsDAO {

	private static int perpage = 9;

	public static List<NewsVO> All_listNews(int startno, String ftype,
			String fkey,int newstype) {
		List<NewsVO> result = null;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setStartno(startno);
		pvo.setFtype(ftype);
		pvo.setFkey(fkey);
		pvo.setPerpage(perpage);
		pvo.setNewstype(newstype);
		
		try {
			result = sess.selectList("NewsMapper.choiceNewsSQL", pvo);
		} finally {
			sess.close();
		}
		return result;
	}
	
	public static List<NewsVO> prvNews(int newstype,int newsno){
		List<NewsVO> result = null;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setNewstype(newstype);
		pvo.setNewsno(newsno);
		try{
			result=sess.selectList("NewsMapper.prevSQL",pvo);
		}finally{
			sess.close();
		}
		return result;
	}
	
	public static List<NewsVO> nextNews(int newstype,int newsno){
		List<NewsVO> result = null;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setNewstype(newstype);
		pvo.setNewsno(newsno);
		try{
			result=sess.selectList("NewsMapper.nextSQL",pvo);
		}finally{
			sess.close();
		}
		return result;
	}
	
	public static List<NewsVO> manyNews(int newstype){
		List<NewsVO> result =null;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setNewstype(newstype);
		
	try{
		result=sess.selectList("NewsMapper.manyNewsSQL",pvo);
	}finally{
		sess.close();
	}
	return result;
	}
		
	public static int totalNews(String ftype, String fkey, int newstype) {

		int result = 0;
		SqlSession sess = MVCSqlSessionFactory.openSession();
		ParamVO pvo = new ParamVO();
		pvo.setFkey(fkey);
		pvo.setFtype(ftype);
		pvo.setNewstype(newstype);
		try {
			result = sess.selectOne("NewsMapper.totalSQL", pvo);
		} finally {
			sess.close();
		}
		return result;
	}

	public static boolean writeNews(NewsVO n) {

		boolean result = false;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			if (sess.insert("NewsMapper.writenewsSQL", n) > 0) {
				result = true;
			}
			sess.commit(); // insert나 update 시 반드시 작성해야함
		} finally {
			sess.close();
		}

		return result;
	}

	public static boolean updateNews(NewsVO n){
		boolean result = false;
		SqlSession sess = null;
		try{
			sess=MVCSqlSessionFactory.openSession();
			if(sess.update("NewsMapper.updatenewsSQL",n)>0){
					result=true;
		}
			sess.commit();
		}finally{
				sess.close();
			}
			return result;
	}
	
	public static NewsVO viewBoard(int newsno) {
		NewsVO result = null;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("NewsMapper.viewnewsSQL", newsno);
		} finally {
			sess.close();
		}
		return result;
	}

	public static int viewCountNews(int newsno) {
		int result = 0;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.update("NewsMapper.viewcountSQL", newsno);
			sess.commit();
		} finally {
			sess.close();
		}
		return result;
	}
	//뉴스 번호 뽑기
	public static String readNewsno() {
		String result = "";
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("NewsMapper.newsnoSQL");
		} finally {
			sess.close();
		}
		return result;
	}
	
	 public static List<NewsVO> readMainNews(String newstype) {
         List<NewsVO> result = null;
         SqlSession sess = null;
         try {
                 sess = MVCSqlSessionFactory.openSession();
                 result = sess.selectList("NewsMapper.readMainNewsSQL", newstype);
         } finally {
                 sess.close();
         }
         return result;
 }
	 
	 public static boolean deleteNews(NewsVO n){
		 boolean result=false;
		 SqlSession sess = null;
		 try{
			 sess=MVCSqlSessionFactory.openSession();
			 result=sess.delete("NewsMapper.deleteSQL",n)>0;
			 sess.commit();	
		 }finally{
			 sess.close();
		 }
		return result;
		 
		 
	 }

	 public static NewsVO readMainNewsImg(String newstype) {
		NewsVO result = null;
		SqlSession sess = null;
		try {
			sess = MVCSqlSessionFactory.openSession();
			result = sess.selectOne("NewsMapper.readMainNewsImgSQL", newstype);
		} finally {
			sess.close();
		}
		return result;
	}

}
